import 'package:flutter/material.dart';

import 'package:aweb5/bean/Shop.dart';
import 'package:aweb5/util/WindowUtil.dart';

class ShoppingCart extends StatefulWidget {
  final callback;

  ShoppingCart(this.callback);
  @override
  State<ShoppingCart> createState() => _ShoppingCartState();
}

class _ShoppingCartState extends State<ShoppingCart>
    with AutomaticKeepAliveClientMixin {
  bool check = false;
  List<Shop> list = List();
  int total = 0;
  int num = 0;
  bool show = false;
  @override
  bool get wantKeepAlive => true;
  @override
  void initState() {
    super.initState();
    for (int i = 0; i < 100; i++) {
      list.add(new Shop("商品 $i", false, 1, 1));
    }

//    Fluttertoast.showToast(msg: "购物车");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          backgroundColor: Colors.white,
          title: Text(
            "购物车",
            style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
          ),
          centerTitle: true,
          elevation: 0,
        ),
        body:body() ,
        bottomSheet: sheet()
    );
  }

  void firedA() {
    widget.callback(1);
  }

  Widget shopItem(index) {
    return Container(
      margin: EdgeInsets.only(left: 10.0, right: 10.0),
      padding: EdgeInsets.only(top: 10.0, bottom: 10.0),
      child: Row(
        children: <Widget>[
          Image.asset(
            "assets/bitmap2.png",
            width: 80.0,
            height: 80.0,
            fit: BoxFit.fill,
          ),
          SizedBox(
            width: 10.0,
          ),
          Column(
            children: <Widget>[
              Text(
                "三养韩国进口火鸡泡面",
                style: TextStyle(color: Color(0xff333333)),
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
              ),
              Text(
                "测试 | 测试",
                style: TextStyle(fontSize: 10.0, color: Color(0xffeeeeee)),
              ),
              Row(
                children: <Widget>[
                  Text("¥${list[index].price}",
                      style: TextStyle(color: Color(0xff333333))),
                  SizedBox(
                    width: 10.0,
                  ),
                  //购物车加减
                  Container(
                    height: 35.0,
//                    decoration: BoxDecoration(
//                        borderRadius: BorderRadius.circular(20.0),
//                        border: Border.all(color: Color(0xffededed))),
                    child: Row(
                      children: <Widget>[
                        GestureDetector(
                          child: Container(
                            width: 22,
                            height: 22,
                            child: Image.asset("assets/jian.png"),
                          ),
                          onTap: () {
                            if (list[index].num > 0) {
                              list[index].num--;
                              total = 0;
                              num = 0;
                              for (Shop shop in list) {
                                if (shop.check) {
                                  total = total + shop.price * shop.num;
                                  num = num + shop.num;
                                }
                              }
                              widget.callback(num);
                              //              Fluttertoast.showToast(msg: "price${list[index].price}num${list[index].num}");
                            }
                            setState(() {});
                          },
                        ),
//                        Container(
//                          height: 30.0,
//                          width: 1.0,
//                          color: Color(0xffededed),
//                        ),

//                        Text("${list[index].num}",style:TextStyle(color: Color(0xffff8900)),),
                        textFiled(index),

//                        Container(
//                          height: 30.0,
//                          width: 1.0,
//                          color: Color(0xffededed),
//                        ),
                        GestureDetector(
                          child: Container(
                            width: 22.0,
                            height: 22.0,
                            child: Image.asset(
                              "assets/jia.png",
                              width: 22.0,
                              height: 22.0,
                            ),
                          ),
                          onTap: () {
                            list[index].num++;
                            num = 0;
                            total = 0;
                            for (Shop shop in list) {
                              if (shop.check) {
                                total = total + shop.price * shop.num;
                                num = num + shop.num;
                              }
                            }
                            widget.callback(num);
                            //  Fluttertoast.showToast(msg: "${list[index].num}");
                            //   Fluttertoast.showToast(msg: "price${list[index].price}num${list[index].num}");
                            setState(() {});
                          },
                        )
                      ],
                    ),
                  )
                ],
              )
            ],
          )
        ],
      ),
    );
  }

  getRightItem(section, index) {
    return Container(
      margin: EdgeInsets.only(left: 0, right: 0),
      height: 100,
      color: Colors.white,
      width: WindowUtil.window_width - 50,
      padding: EdgeInsets.all(10),
      child: Row(
        children: <Widget>[
          Image.asset(
            "assets/banner1.png",
            width: 80,
            height: 80,
            fit: BoxFit.fill,
          ),
          SizedBox(
            width: 10,
          ),
          Expanded(child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[

                  Text(
                  "好喝的牛奶",
                  style: TextStyle(color: Colors.black, fontSize: 14),
                  overflow: TextOverflow.ellipsis,
                  maxLines: 2,
                ),

              Expanded(child: Text("")),

                    Row(
                    children: <Widget>[
                      Column(
                        children: <Widget>[
                          Text(
                            "规格",
                            style: TextStyle(
                                fontSize: 9, color: Color(0xff888888)),
                          ),
                          Text(
                            "1",
                            style: TextStyle(
                                color: Color(0xffff0036), fontSize: 16),
                          )
                        ],
                      ),
                      Expanded(child: Text("")),
                      Container(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: <Widget>[
                            GestureDetector(
                              child: Container(
                                width: 22,
                                height: 22,
                                child: Image.asset("assets/jian.png"),
                              ),
                              onTap: () {
                                if (list[index].num > 0) {
                                  list[index].num--;
                                  total = 0;
                                  num = 0;
                                  for (Shop shop in list) {
                                    if (shop.check) {
                                      total = total + shop.price * shop.num;
                                      num = num + shop.num;
                                    }
                                  }
                                  widget.callback(num);
                                  //              Fluttertoast.showToast(msg: "price${list[index].price}num${list[index].num}");
                                }
                                setState(() {});
                              },
                            ),
                            textFiled(index),
                            GestureDetector(
                              child: Container(
                                width: 22.0,
                                height: 22.0,
                                child: Image.asset(
                                  "assets/jia.png",
                                  width: 22.0,
                                  height: 22.0,
                                ),
                              ),
                              onTap: () {
                                list[index].num++;
                                num = 0;
                                total = 0;
                                for (Shop shop in list) {
                                  if (shop.check) {
                                    total = total + shop.price * shop.num;
                                    num = num + shop.num;
                                  }
                                }
                                widget.callback(num);
                                //  Fluttertoast.showToast(msg: "${list[index].num}");
                                //   Fluttertoast.showToast(msg: "price${list[index].price}num${list[index].num}");
                                setState(() {});
                              },
                            )
                          ],
                        ),
                      )
                    ],
                  )
            ],
          )),

        ],
      ),
    );
  }

  Widget textFiled(index) {
    TextEditingController textEditingController = TextEditingController();
    return Container(
      padding: EdgeInsets.only(top: 10),
      width: 30,
      height: 40,
      child: TextField(
          textAlign: TextAlign.center,
          controller: textEditingController,
          style: TextStyle(color: Color(0xffff8900)),
          onChanged: (v) {
            textEditingController.text = v;
            if (v.isNotEmpty) {
              list[index].num = int.parse(v);
            }
          },
          keyboardType: TextInputType.numberWithOptions(),
          decoration: InputDecoration(
              contentPadding: EdgeInsets.all(0),
              hintText: '${list[index].num}',
              border: InputBorder.none,
              hintStyle: TextStyle(color: Color(0xffff8900)))),
    );
  }

  Widget buildList() {
    return ListView.builder(
       // padding: EdgeInsets.only(top: 50),
        // physics: NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        itemCount: list.length,
        itemBuilder: (_, int id) {
          return new Row(
            children: <Widget>[
              Opacity(
                  opacity: show ? 1 : 0,
                  child: Container(
                    width: 50,
                    child: Checkbox(
                        value: list[id].check,
                        onChanged: (check) {
                          list[id].check = check;
                          total = 0;
                          num = 0;
                          for (Shop shop in list) {
                            if (shop.check) {
                              total = total + shop.price * shop.num;
                              num = num + shop.num;
                            }
                          }
                          widget.callback(num);
                          //     Fluttertoast.showToast(msg: "price${list[id].price}num${list[id].num}");
                          //   firedA();
                          setState(() {});
                        }),
                  )),

//                  Text("${list[id].name}   价格${list[id].price}")
              //shopItem(id)
              getRightItem(_, id)
            ],
          );
        });
  }
  Widget edit(){
    return Container(
      width: WindowUtil.window_width,
      height: 38,
      alignment: Alignment.centerRight,
      child:GestureDetector(
        child:  Container(
          alignment: Alignment.center,
          child: Text(
            "编辑",
            style: TextStyle(color: Color(0xff2d2d2d), fontSize: 12),
            textAlign: TextAlign.center,
          ),
          width: 60,
          height: 20,
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(20),
              border: Border.all(color: Color(0xff2d2d2d))),
        ),
        onTap: (){
          show=!show;
          setState(() {

          });
        },
      )
      ,
    );
  }
  Widget divider(){
    return Container(
      height: 10,
      width: WindowUtil.window_width,
      color: Color(0xfff5f5f5),
    );
  }
  Widget body(){
      if(false){
        return
          Column(
            // physics: NeverScrollableScrollPhysics(),
            children: <Widget>[
              //Positioned(child: Text("1"),top: 50,left: 10,),
              Container(
                height: 1,
                width: WindowUtil.window_width,
                color: Color(0xfff5f5f5),
              ),
              edit(),
              divider(),
              Expanded(child: buildList()),
            ],
          );


      }else{
        return
          Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Image.asset("assets/gouwuche_kongbai.png",width: 145,height: 102,),
                SizedBox(height: 31,),
                Text("购物车是空的~",style: TextStyle(color: Color(0xfff8a84c),fontSize: 16),)
              ],

            ),
          );
          ;

      }


  }
  Widget sheet(){
    if(true){
      return Text("");
    }else{
      return
        Container(
          height: 49.0,
          // padding: EdgeInsets.only(left: 10.0, right: 10.0),
          child: Row(

            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Expanded(
                  flex: 2,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: <Widget>[
                      Checkbox(
                          value: check,
                          onChanged: (check) {
                            total = 0;
                            this.check = check;
                            for (Shop shop in list) {
                              shop.check = check;
                            }
                            for (Shop shop in list) {
                              if (shop.check) {
                                total = total + shop.price;
                              }
                            }
                            setState(() {});
                          }),

                      Text("全选",style: TextStyle(color: Color(0xff2d2d2d),fontSize: 12),),
                      Text(" 合计",style: TextStyle(color: Color(0xff2d2d2d),fontSize: 13,fontWeight: FontWeight.bold),),

                      Text(
                        "¥$total",
                        style: TextStyle(color: Colors.red,fontSize: 16),
                      )
                    ],

                  )),
              Expanded(
                  flex: 2,
                  child:  Container(

                    height: 49,
                    decoration: BoxDecoration(
                        color: Color(0xffff8900)),

                    child: Column(
                      children: <Widget>[
                        Text(
                          "去结算",
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 16.0,
                          ),
                        ),
                        Text("(不含运费)",style: TextStyle(
                          color: Colors.white,
                          fontSize: 10.0,
                        ))
                      ],
                    ),
                  ))
              ,
            ],
          ),
        );
    }
  }


}
